package EnumBoost;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * Created by huxin on 2017/2/21.
 */
public enum  ByteEnumBoost {
    ;

    private Byte Value;

    private static class MapValue {
        public static HashMap<Byte, ByteEnumBoost> map = new LinkedHashMap<>();
    }

    public static ByteEnumBoost getLegNO(Byte value) {

        ByteEnumBoost bEnumBoost = ByteEnumBoost.MapValue.map.get(value);
        if (bEnumBoost == null) {
            try {
                throw new Exception("get LegNO Error! value: " + value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return bEnumBoost;
    }

    ByteEnumBoost(Byte i) {
        ByteEnumBoost bEnumBoost = ByteEnumBoost.MapValue.map.get(i);
        if (bEnumBoost != null) {
            try {
                throw new Exception("ByteEnumBoost Exist Index: " + i + " legNo: " + bEnumBoost.toString()
                        + ". Error: This legNO: " + this.toString() + " Index: " + i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            this.Value = i;
            ByteEnumBoost.MapValue.map.put(this.Value, this);
        }
    }

    ByteEnumBoost() {
        Set<Byte> byteSet = ByteEnumBoost.MapValue.map.keySet();
        int size = byteSet.size();
        if(size == 0)
            this.Value = 0;
        else {
            //使用的是最大的Key值来设置默认构造
            Byte key = (Byte) byteSet.toArray()[size - 1];

            int value = key + 1;
            if( value > Byte.MAX_VALUE )
            {
                try {
                    throw new Exception("ByteEnumBoost default construct error: " + value + " legNo: " + this.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                this.Value = (byte)value;
            }
        }
        ByteEnumBoost.MapValue.map.put(this.Value, this);
    }

    public Byte getValue() {
        return Value;
    }
}
